//
//  WmcButtonBubbleView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/25.
//

import UIKit
import RxRelay

extension WmcButtonBubbleView {
    
    enum ArrowDirection {
        case up
        case down
    }
}

class WmcButtonBubbleView: WmcBaseView {

    private lazy var arrowView: UIImageView = {
        let imgView = UIImageView()
        return imgView
    }()
    
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.wmc_white_FFFFFF
        view.layer.cornerRadius = 8.0
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var msgLabel: UILabel = {
        let label = UILabel()
        label.font = 15.wmc_scaleFont(weight: .medium)
        label.textColor = UIColor.wmc_black85
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var okButton: UIButton = {
        let button = UIButton()
        button.setTitle("我知道了", for: .normal)
        button.setTitleColor(UIColor.wmc_mainTheme, for: .normal)
        button.titleLabel?.font = 15.wmc_scaleFont(weight: .medium)
        button.layer.cornerRadius = 4.0
        button.layer.borderWidth = 1.0
        button.layer.borderColor = UIColor.wmc_mainTheme.cgColor
        return button
    }()
    
    let arrowDirection: ArrowDirection
    
    var message: String = "" {
        didSet {
            msgLabel.text = message
        }
    }
    var buttonTitle: String = "" {
        didSet {
            okButton.setTitle(buttonTitle, for: .normal)
        }
    }
    
    var buttonW: CGFloat = 0.0 {
        didSet {
            okButton.snp.updateConstraints { make in
                make.width.equalTo(buttonW)
            }
        }
    }
    
    var arrowOffsetX: CGFloat = 0.0 {
        didSet {
            arrowView.snp.updateConstraints { make in
                make.centerX.equalToSuperview().offset(arrowOffsetX)
            }
        }
    }
    /// 点击按钮回调
    let okButtonAction: PublishRelay<Void> = .init()
    
    // MARK: Init
    init(arrowDirection: ArrowDirection) {
        self.arrowDirection = arrowDirection
        super.init(frame: .zero)
    }
    
    public required convenience init() {
        self.init(arrowDirection: .up)
    }
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        
        let padding = 12.wmc_autoHor
        let arrowW = 16.wmc_autoHor
        let arrowH = 8.wmc_autoHor
        
        backgroundColor = UIColor.clear
        // 箭头
        addSubview(arrowView)
        arrowView.snp.makeConstraints { make in
            make.width.equalTo(arrowW)
            make.height.equalTo(arrowH)
            make.centerX.equalToSuperview().offset(arrowOffsetX)
        }
        // contentView
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
        }
        // msgLabel
        contentView.addSubview(msgLabel)
        msgLabel.snp.makeConstraints { make in
            make.top.equalTo(padding)
            make.left.equalTo(padding)
            make.right.equalTo(-padding)
        }
        // okButton
        contentView.addSubview(okButton)
        okButton.snp.makeConstraints { make in
            make.top.equalTo(msgLabel.snp.bottom).offset(padding)
            make.right.equalTo(msgLabel)
            make.bottom.equalTo(-padding)
            make.width.equalTo(0.0)
            make.height.equalTo(32.0.wmc_autoHor)
        }
        
        switch arrowDirection {
        case .up:
            arrowView.image = "icon_triangle_up_16_8".wmc_image
            arrowView.snp.makeConstraints { make in
                make.top.equalToSuperview()
            }
            contentView.snp.makeConstraints { make in
                make.top.equalTo(arrowView.snp.bottom)
                make.bottom.equalToSuperview()
            }
        case .down:
            arrowView.image = "icon_triangle_down_16_8".wmc_image
            contentView.snp.makeConstraints { make in
                make.top.equalToSuperview()
            }
            arrowView.snp.makeConstraints { make in
                make.top.equalTo(contentView.snp.bottom)
                make.bottom.equalToSuperview()
            }
        }
    }
    
    // MARK: Bind Action
    override func bindViewAction() {
        super.bindViewAction()
        okButton.rx.tap.bind(to: okButtonAction).disposed(by: rx.disposeBag)
    }
}
